import { RouteObject } from 'react-router-dom';
import {
  lazyload
} from '@/utils/tools';
import Loyout from '@/layout';
import Login from '@/pages/login';
import NoPage from '@/pages/noPage';
import './styles.less'

const exemples = import.meta.glob('../pages/*/index.tsx')

export const exemplesNameList: { path: string, name: string }[] = Object.keys(exemples).map(key => {
  const keyArr = key.split('/')
  const path = `/${keyArr[1]}/${keyArr[2]}`
  return {
    path,
    name: keyArr[2]
  }
})

const WhiteList = [
  'login'
]

const children: RouteObject[] = Object.keys(exemples)
  .filter((key) => WhiteList.find((name) => !new RegExp(name, 'g').test(key)))
  .map(key => {
    const Component = lazyload(exemples[key])
    const keyArr = key.split('/')
    const path = `/${keyArr[2]}`
    return {
      path,
      element: <Component />,
    }
  })


export const routes: RouteObject[] = [
  {
    path: '/',
    element: <Loyout />,
    children: [
      ...children,
      {
        path: '*',
        element: <NoPage />,
      },
    ]
  },
  {
    path: '/login',
    element: <Login />,
  }
];


